package main

import (
	"fmt"
	"log"
	"os"
	"runtime"
)

//使用标准的logger，通常是把配置写在init函数中
func init() {
	var logFile *os.File
	logFile, err := os.OpenFile("./log.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
	if err != nil {
		fmt.Println("open log file failed, err:", err)
		return
	}
	defer logFile.Close()
	log.SetOutput(logFile)
	log.SetFlags(log.Llongfile | log.Lmicroseconds | log.Ldate)
}

func main() {
	var a int
	a = 1110
	log.Println("log 日志", a)
	var logFile *os.File
	logFile, err := os.OpenFile("./log.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
	if err != nil {
		fmt.Println("open log file failed, err:", err)
		return
	}
	defer logFile.Close()
	log.SetOutput(logFile)
	log.SetFlags(log.Llongfile | log.Lmicroseconds | log.Ldate)
	log.Println("这是一条很普通的日志。")
	log.SetPrefix("[小王子]")
	log.Println("这是一条很普通的日志。")

	pc, file, line, ok := runtime.Caller(0)
	if !ok {
		fmt.Println("caller failed")
		return
	}
	funcName := runtime.FuncForPC(pc).Name()

	fmt.Println("funcName:", funcName)
	fmt.Println("file :", file)
	fmt.Println("line:", line)

}